package cn.wolfcode.car.business.service.impl;

import cn.wolfcode.car.business.domain.Appointment;
import cn.wolfcode.car.business.mapper.AppointmentMapper;
import cn.wolfcode.car.business.query.AppointmentQuery;
import cn.wolfcode.car.business.service.IAppointmentService;
import cn.wolfcode.car.common.base.page.TablePageInfo;
import cn.wolfcode.car.common.exception.BusinessException;
import cn.wolfcode.car.common.util.Convert;
import com.github.pagehelper.PageHelper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;

@Service
@Transactional
public class AppointmentServiceImpl implements IAppointmentService {

    @Autowired
    private AppointmentMapper appointmentMapper;



    @Override
    public TablePageInfo<Appointment> query(AppointmentQuery qo) {
        PageHelper.startPage(qo.getPageNum(), qo.getPageSize());
        return new TablePageInfo<Appointment>(appointmentMapper.selectForList(qo));
    }




    @Override
    public Appointment get(Long id) {
        return appointmentMapper.selectByPrimaryKey(id);
    }

    @Override
    public void save(Appointment appointment) {
        appointment.setAppointmentTime(new Date());
        //将状态改为预约中
        appointment.setStatus(Appointment.STATUS_APPOINTMENT);
        appointmentMapper.insert(appointment);
    }


    @Override
    public void update(Appointment appointment) {
        //预约中才能修改
        if (appointment==null||!Appointment.STATUS_APPOINTMENT.equals(appointment.getStatus())) {
            throw new BusinessException("必须是预约中才能修改");
        }
        appointmentMapper.updateByPrimaryKey(appointment);

    }

    @Override
    public void deleteBatch(String ids) {
        Long[] dictIds = Convert.toLongArray(ids);
        for (Long dictId : dictIds) {
            appointmentMapper.deleteByPrimaryKey(dictId);
        }
    }

    @Override
    public List<Appointment> list() {
        return appointmentMapper.selectAll();
    }


    @Override
    public void cancel(Long id) {

        //限制状态--必须是预约中才可以取消
        Appointment appointment = appointmentMapper.selectByPrimaryKey(id);
        System.out.println("状态"+appointment.getStatus());
        if (appointment==null||!Appointment.STATUS_APPOINTMENT.equals(appointment.getStatus())) {
            throw new BusinessException("必须是预约中才可以取消预约");
        }
        //修改预约记录的状态为用户取消
        appointmentMapper.changeStatus(id,Appointment.STATUS_CANCEL);
    }

    @Override
    public void arrival(Long id) {
        //限制状态--必须是预约中
        Appointment appointment = appointmentMapper.selectByPrimaryKey(id);
        System.out.println("状态"+appointment.getStatus());
        if (appointment==null||!Appointment.STATUS_APPOINTMENT.equals(appointment.getStatus())) {
            throw new BusinessException("必须是预约中才可以设置到店状态");
        }
        appointmentMapper.arrival(id,Appointment.STATUS_ARRIVAL);
    }

    @Override
    public void changeStatus(Long id, Integer status) {
        appointmentMapper.changeStatus(id,Appointment.STATUS_SETTLE);
    }
}
